package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.dao.TravelingArticleImageDAO;
import bist.believeinstep.domain.TravelingArticleImage;
import bist.believeinstep.mapper.TravelingArticleImageMapper;

public class TravelingArticleImageTemplate implements TravelingArticleImageDAO, ConnectionDAO {
	private JdbcTemplate jdbctemplate;
	
	@Override
	public void setDataSource(DataSource dataSource){
		this.jdbctemplate = new JdbcTemplate(dataSource);
	}
	@Override
	public void create(TravelingArticleImage travelingArticleImage){
		String query = "INSERT INTO bist_traveling_article_images ("
				+ "bist_traveling_articles_id, bist_images_id, created_date, modified_date,"
				+ "created_by, modified_by) VALUES (?,?,?,?,?,?)";
		try{
			jdbctemplate.update(query,new Object[]{
					travelingArticleImage.getTravelingArticleId(),
					travelingArticleImage.getImageId(),
					travelingArticleImage.getCreated(),
					travelingArticleImage.getModified(),
					travelingArticleImage.getCreatedBy(),
					travelingArticleImage.getModifiedBy()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void update(TravelingArticleImage travelingArticleImage){
		String query = "UPDATE bist_traveling_article_images SET"
				+ "bist_traveling_articles_id = ?"
				+ "bist_images_id = ?"
				+ "created_date = ?"
				+ "modified_date = ?"
				+ "created_by = ?"
				+ "modified_by = ?";
		try{
			jdbctemplate.update(query, new Object[]{
				travelingArticleImage.getTravelingArticleId(),
				travelingArticleImage.getImageId(),
				travelingArticleImage.getCreated(),
				travelingArticleImage.getModified(),
				travelingArticleImage.getCreatedBy(),
				travelingArticleImage.getModifiedBy()			
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(Integer id){
		String query = "DELETE FROM bist_traveling_article_images WHERE id = ?";
		try{
			jdbctemplate.update(query, new Object[]{id});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public List<TravelingArticleImage> list(){
		String query = "SELECT * FROM bist_traveling_article_images";
		try{
			List<TravelingArticleImage> travelingArticleImage = jdbctemplate.query(query, 
					new TravelingArticleImageMapper());
			return travelingArticleImage;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		return null;
	}
}
